package com.wb.web.controller.question;


import com.wb.common.core.controller.BaseController;
import com.wb.common.core.domain.AjaxResult;
import com.wb.common.core.page.TableDataInfo;
import com.wb.common.vo.DeleteRequest;
import com.wb.common.vo.ExamsLevelModelVO;
import com.wb.common.vo.QuestionModelVO;
import com.wb.system.domain.ExamsLevelEntity;
import com.wb.system.domain.QuestionEntity;
import com.wb.system.service.ExamsLevelService;
import com.wb.system.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("question")
@Api(tags = "题目主表（题库）")
public class QuestionController extends BaseController {
    @Autowired
    private QuestionService questionService;

    @PostMapping("/list")
    @ApiOperation("题目主表列表")
    public TableDataInfo list(@RequestBody QuestionModelVO questionModelVO) {
        startPage();
        List<QuestionEntity> list = questionService.selectQuestionList(questionModelVO);
        return getDataTable(list);
    }


    @GetMapping("/{id}")
    @ApiOperation("获取题目主表详情")
    public AjaxResult get(@PathVariable("id") Long id) {
        return AjaxResult.success(questionService.selectById(id));
    }

    @PostMapping("/add")
    @ApiOperation("题目主表创建")
    public AjaxResult add(@Valid @RequestBody QuestionEntity questionEntity) {
        QuestionEntity questionEntityR = new QuestionEntity();
        BeanUtils.copyProperties(questionEntity, questionEntityR);
        questionService.saveQuestion(questionEntityR);
        return AjaxResult.success();
    }


    @PostMapping("/update")
    @ApiOperation("更新题目主表")
    public AjaxResult update(@RequestBody QuestionEntity questionEntityVO) {
        QuestionEntity questionEntity = new QuestionEntity();
        BeanUtils.copyProperties(questionEntityVO, questionEntity);
      //  ExamsLevelEntity.setUpdateBy(getUsername());
        questionService.updateQuestionById(questionEntity);
        return AjaxResult.success();
    }

    @DeleteMapping("/deleteAll")
    @ApiOperation("删除All")
    public AjaxResult deleteAll(@RequestBody DeleteRequest request) {
        questionService.removeQuestionByIds(request.getIds());
        return AjaxResult.success();
    }
}
